#pragma once
#include "DirectXTK\GeometricPrimitive.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
using namespace DirectX;
class Mesh 
{
public:
	Mesh(const std::string& path) 
	{
		Assimp::Importer imp;
		const auto pScene = imp.ReadFile(path.c_str(),
			aiProcess_Triangulate |
			aiProcess_JoinIdenticalVertices |
			aiProcess_ConvertToLeftHanded |
			aiProcess_GenNormals |
			aiProcess_CalcTangentSpace
		);

		if (pScene == nullptr)
		{
			assert(false);
		}
		const auto& aiMesh = *pScene->mMeshes[0];
		// vertex
		for (size_t i = 0; i < aiMesh.mNumFaces; i++)
		{
			for (size_t j = 0; j < 3; j++)
			{
				indices.push_back(aiMesh.mFaces[i].mIndices[j]);
			}
		}

		for (size_t i = 0; i < aiMesh.mNumVertices; i++)
		{
			GeometricPrimitive::VertexType vertex;
			vertex.position = { aiMesh.mVertices[i].x,aiMesh.mVertices[i].y, aiMesh.mVertices[i].z };

			if (aiMesh.HasNormals())
			{
				const auto& normal = aiMesh.mNormals[i];
				vertex.normal = { normal.x,normal.y,normal.z };
			}

			/*if (aiMesh.HasTangentsAndBitangents())
			{
				const auto& tangent = aiMesh.mTangents[i];
				vertex.TangentU = { tangent.x,tangent.y,tangent.z };
			}*/

			if (aiMesh.HasTextureCoords(0))
			{
				const auto& uv = aiMesh.mTextureCoords[0];
				vertex.textureCoordinate = { uv->x,uv->y };
			}
			vertices.push_back(vertex);
		}
	}
	~Mesh() = default;
	const GeometricPrimitive::VertexCollection& GetVertices() const { return vertices; }
	const GeometricPrimitive::IndexCollection& GetIndices() const { return indices; }
private:
	GeometricPrimitive::VertexCollection vertices;
	GeometricPrimitive::IndexCollection indices;
};